var Rx = require("rxjs");
var operators = require("rxjs/operators");
var SERVER_CONST = require("../server-const");
var express = require("express");
var router = express.Router();
var model = require("../model/chatModel");
var collectionModel = require("../model/collectionModel");
var taskModel = require("../model/taskModel");

var user = model.user;
var task = taskModel.task;
var channels = model.channels;
var teams = model.teams;
var channelMembers = model.channelMembers;
var teamMembers = model.teamMembers;
var collectionItem = collectionModel.collectionItem;
var label = collectionModel.label;
var util = require("../server-util/util");
var mongoose = require("mongoose");
const globalVar = require("../server-util/globalVar");
const { padStart } = require("lodash");
var post = model.post;
router.get("/", function (req, res, next) {
	res.send("respond with a resource");
});

//删除一条记录
router.delete("/remove", util.checkAuth, function (req, res, next) {
	let CreatorId = mongoose.Types.ObjectId(req.param("CreatorId"));
	let _id = mongoose.Types.ObjectId(req.param("_id"));

	task
		.deleteOne({
			CreatorId,
			_id,
		})
		.exec(function (err, result) {
			if (!err) {
				util.res.success({
					res,
					data: result,
				});
			} else {
				util.res.error({
					res,
					msg: err,
				});
			}
		});
});

/**
 * @api {POST} /account/addTitle 新增账户标题
 * @apiSampleRequest off
 * @apiParam (params) {String} UserId
 * @apiParam (params) {String} UserId
 * @apiGroup account
 */
router.post("/addTask", util.checkAuth, function (req, res, next) {
	let CreatorId = mongoose.Types.ObjectId(req.body.CreatorId);
	let Title = req.body.Title;
	let State = req.body.State;
	let Message = req.body.Message;
	let NotificationId = req.body.NotificationId;
	let CreateAt = new Date().getTime();
	let ExpirationTime = req.body.ExpirationTime;
	let UpdateAt = null;
	let OrderNum = 0;
	let StartTime = req.body.StartTime;
	let CalendarId = req.body.CalendarId;
	let EventId = req.body.EventId;
	let CalendarName = req.body.CalendarName;

	new task({
		ExpirationTime,
		Title,
		State,
		Message,
		NotificationId,
		CreateAt,
		UpdateAt,
		OrderNum,
		CreatorId,
		StartTime,
		CalendarId,
		EventId,
		CalendarName,
	}).save(function (err, result) {
		if (err) {
			util.res.error({
				res,
				msg: "服务器报错",
			});
		} else {
			util.res.success({
				res,
				data: result,
			});
		}
	});
});

//更新任务
router.post("/updateTask", util.checkAuth, function (req, res, next) {
	let CreatorId = mongoose.Types.ObjectId(req.body.CreatorId);
	let _id = mongoose.Types.ObjectId(req.body._id);
	let Title = req.body.Title;
	let State = req.body.State;
	let Message = req.body.Message;
	let NotificationId = req.body.NotificationId;
	let ExpirationTime = req.body.ExpirationTime;
	let StartTime = req.body.StartTime;
	let CalendarId = req.body.CalendarId;
	let EventId = req.body.EventId;
	let CalendarName = req.body.CalendarName;
	let UpdateAt = new Date().getTime();


	task
		.update(
			{
				CreatorId,
				_id,
			},
			{
				$set: {
					Title,
					State,
					Message,
					NotificationId,
					ExpirationTime,
					UpdateAt,
					StartTime,
					CalendarId,
					EventId,
					CalendarName,
				},
			}
		)
		.exec(function (err, result) {
			if (err) {
				util.res.error({
					res,
					msg: err,
				});
			} else {
				util.res.success({
					res,
					data: result,
				});
			}
		});
});

//查询任务
router.get("/get", util.checkAuth, function (req, res, next) {
	let CreatorId = mongoose.Types.ObjectId(req.query.CreatorId);
	task
		.aggregate([
			{
				$match: {
					CreatorId: CreatorId,
				},
			},
		])
		.exec(function (err, result) {
			if (err) {
				util.res.error({
					res,
					msg: "服务器报错",
				});
			} else {
				util.res.success({
					res,
					data: result,
				});
			}
		});
});

module.exports = router;
